package com.woniu.controller;


import com.woniu.entity.PageData;
import com.woniu.entity.Result;
import com.woniu.entity.dto.QueryCarDTO;
import com.woniu.entity.vo.OrderByIdVO;
import com.woniu.entity.vo.QueryCarByIdVO;
import com.woniu.entity.vo.QueryCarVO;
import com.woniu.pojo.Order;
import com.woniu.service.CarService;
import com.woniu.utils.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/car")
@Api("车辆管理")
public class CarController extends BaseController {
    @Autowired
    private CarService carService;

    @ApiOperation("分页+搜索")
    @PostMapping("/list/search/{currentPage}/{pageSize}")
    public Result listSearch(@PathVariable Integer currentPage,
                             @PathVariable Integer pageSize,
                             @RequestBody QueryCarDTO queryCarDTO){
        PageData pageData = carService.listSearch(currentPage, pageSize, queryCarDTO);
        return toDataResult(pageData);
    }


    @ApiOperation("车辆禁用")
    @PostMapping("/disabledCar/{id}/{status}")
    public Result disabledUser(@PathVariable String id,@PathVariable Integer status){
        if (status>1){
            return error("禁用失败");
        }
        int i = carService.disabledCar(id, status);
        if (i>0) {
            return success("操作成功");
        }else {
            return error("操作失败");
        }
    }

    @ApiOperation("车俩删除")
    @GetMapping("/deleteCar/{id}")
    public Result deleteCar(@PathVariable String id){
        boolean i = carService.removeById(id);
        if (i) {
            return success("操作成功");
        }else {
            return error("操作失败");
        }
    }

    /** 用户详情使用到此API */
    @ApiOperation("根据用户ID查询车俩")
    @GetMapping("/CarByUserId/{id}")
    public Result CarByUserId(@PathVariable String id){
        List<QueryCarByIdVO> queryCarVOList = carService.listCarByUserId(id);
        return toDataResult(queryCarVOList);
    }

    @ApiOperation("根据车辆ID查询订单")
    @GetMapping("/QueryOrderByCarId/{id}")
    public Result QueryOrderByCarId(@PathVariable String id){
        List<OrderByIdVO> queryCarVOList = carService.listOrderByCarId(id);
        return toDataResult(queryCarVOList);
    }
}
